/*
 * Copyright (c) 2015, Freescale Semiconductor, Inc.
 * Copyright 2016-2020 NXP
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "pin_mux.h"
#include "clock_config.h"
#include "board.h"
#include "fsl_debug_console.h"
#include "fsl_gpio.h"
#include "cmsis_os.h"
#include "fsl_lpuart.h"
#include "sal_module_wrapper.h"
#include "ec600s.h"
#include "tos_at.h"

/*******************************************************************************
 * Definitions
 ******************************************************************************/
#define EXAMPLE_LED_GPIO     BOARD_USER_LED_GPIO
#define EXAMPLE_LED_GPIO_PIN BOARD_USER_LED_GPIO_PIN

/*******************************************************************************
 * Prototypes
 ******************************************************************************/

/*******************************************************************************
 * Variables
 ******************************************************************************/
/* The PIN status */
volatile bool g_pinSet = false;
/*******************************************************************************
 * Code
 ******************************************************************************/

uint8_t data;
#define APPLICATION_TASK_STK_SIZE       4096
void application_entry(void *arg);
osThreadDef(application_entry, osPriorityNormal, 1, APPLICATION_TASK_STK_SIZE);

#define DISP_TASK_STK_SIZE       409600/2
void display_entry(void *arg);
osThreadDef(display_entry, osPriorityNormal, 1, DISP_TASK_STK_SIZE);

void LPUART4_IRQHandler(void)
{


    /* If new data arrived. */
    if ((kLPUART_RxDataRegFullFlag)&LPUART_GetStatusFlags(LPUART4))
    {
        data = LPUART_ReadByte(LPUART4);
        tos_at_uart_input_byte(data);
    }
    SDK_ISR_EXIT_BARRIER;
}

void LPUART2_IRQHandler(void)
{


    /* If new data arrived. */
    if ((kLPUART_RxDataRegFullFlag)&LPUART_GetStatusFlags(LPUART2))
    {
        data = LPUART_ReadByte(LPUART2);
        tos_at_uart_input_byte(data);
    }
    SDK_ISR_EXIT_BARRIER;
}


extern void application_entry(void *arg);
extern void display_entry(void *arg);


/*!
 * @brief Main function
 */
int main(void)
{
    /* Define the init structure for the output LED pin*/
    gpio_pin_config_t led_config = {kGPIO_DigitalOutput, 0, kGPIO_NoIntmode};
    lpuart_config_t config;
    /* Board pin, clock, debug console init */
    BOARD_ConfigMPU();
    BOARD_InitPins();



    BOARD_BootClockRUN();
    BOARD_InitDebugConsole();
#if 0
    LPUART_GetDefaultConfig(&config);
    config.baudRate_Bps = BOARD_DEBUG_UART_BAUDRATE;
    config.enableTx     = true;
    config.enableRx     = true;

    LPUART_Init(LPUART2, &config, BOARD_DebugConsoleSrcFreq());

    /* Enable RX interrupt. */
    LPUART_EnableInterrupts(LPUART2, kLPUART_RxDataRegFullInterruptEnable);
    EnableIRQ(LPUART2_IRQn);

#endif

    /* Print a note to terminal. */
    PRINTF("Hello world from i.MX RT1060.\r\n");
    PRINTF("Welcome to TencentOS tiny\r\n");
    PRINTF("cpu clk:%d\r\n",SystemCoreClock);
    /* Init output LED GPIO. */
    GPIO_PinInit(BOARD_INITPINS_LED_GPIO, BOARD_INITPINS_LED_GPIO_PIN, &led_config);

    osKernelInitialize(); // TencentOS Tiny kernel initialize
    //osThreadCreate(osThread(application_entry), NULL); // Create TencentOS Tiny task
    osThreadCreate(osThread(display_entry), NULL); // Create TencentOS Tiny task
    osKernelStart(); // Start TencentOS Tiny

    while (1)
    {
        SDK_DelayAtLeastUs(100000, SDK_DEVICE_MAXIMUM_CPU_CLOCK_FREQUENCY);
        PRINTF("\r\n The LED ONNNNNis blinking.\r\n");
#if (defined(FSL_FEATURE_IGPIO_HAS_DR_TOGGLE) && (FSL_FEATURE_IGPIO_HAS_DR_TOGGLE == 1))
        GPIO_PortToggle(BOARD_INITPINS_LED_GPIO, 1u << BOARD_INITPINS_LED_GPIO_PIN);
#else
        if (g_pinSet)
        {
            GPIO_PinWrite(BOARD_INITPINS_LED0_GPIO, BOARD_INITPINS_LED0_GPIO_PIN, 0U);
            g_pinSet = false;
        }
        else
        {
            GPIO_PinWrite(BOARD_INITPINS_LED0_GPIO, BOARD_INITPINS_LED0_GPIO_PIN, 1U);
            g_pinSet = true;
        }
#endif /* FSL_FEATURE_IGPIO_HAS_DR_TOGGLE */
    }
}
